package com.xuan.chatroom.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.io.Encoders;
import io.jsonwebtoken.security.Keys;
import org.junit.jupiter.api.Test;

import javax.crypto.SecretKey;
import java.security.Key;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {


    private final static String secretString = "Gl9K+x1XywFg+UoZBK5Ifiai4wbBBUsqdstDVzLvy+0=";    // 秘钥
    private final static Key key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secretString));


    // 生成令牌
    @Test
    public void geneToken() {
        Map<String, Object> claim = new HashMap<>();
        claim.put("id", 1);
        claim.put("username", "zhangsan");

        String token = Jwts.builder()
                .addClaims(claim)
                .signWith(key)
                .compact();

        System.out.println(token);
    }

    // 生成秘钥
    @Test
    public void geneKey() {
        // 生成key
        SecretKey secretKey = Keys.secretKeyFor(SignatureAlgorithm.HS256);
        String encode = Encoders.BASE64.encode(secretKey.getEncoded());
        System.out.println(encode);
    }


    // 校验令牌
    public void parseToken() {

    }
}
